package com.tianji.promotion.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.dto.CouponDiscountDTO;
import com.tianji.promotion.domain.dto.OrderCouponDTO;
import com.tianji.promotion.domain.dto.OrderCourseDTO;
import com.tianji.promotion.domain.query.UserCouponQuery;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.IDiscountService;
import com.tianji.promotion.service.IUserCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户领取优惠券的记录，是真正使用的优惠券信息 前端控制器
 * </p>
 *
 * @author Yandel
 * @since 2025-08-17
 */
@Api(tags = "优惠券相关接口")
@RestController
@RequestMapping("/user-coupons")
public class UserCouponController {

    @Resource
    private IUserCouponService userCouponService;

    @Resource
    private IDiscountService discountService;

    @ApiOperation("领取优惠券接口")
    @PostMapping("/{couponId}/receive")
    public void receiveCoupon(@PathVariable("couponId") Long couponId){
        userCouponService.receiveCoupon(couponId);//TODO 实现基于lua脚本的异步领券
    }

    //TODO 完善兑换优惠券功能
    //TODO 异步的兑换码领券
    @ApiOperation("兑换码兑换优惠券接口")
    @PostMapping("/{code}/exchange")
    public void exchangeCoupon(@PathVariable("code") String code){
        userCouponService.exchangeCoupon(code);
    }

    @ApiOperation("查询我的优惠券")
    @GetMapping("/page")
    public PageDTO<CouponVO> queryUserCoupons(UserCouponQuery query){
        //TODO 完成测试
        return userCouponService.queryUserCoupons(query);
    }

    //TODO 实现用短信提醒用户使用优惠券的功能【非重要】


    /**
     * 该方法是给tj-trade服务，远程调用使用
     * @param
     * @return
     */
    @ApiOperation("查询可用优惠券")
    @PostMapping("/available")
    public List<CouponDiscountDTO> findDiscountSolution(@RequestBody List<OrderCourseDTO> orderCourses){
        return discountService.findDiscountSolution(orderCourses);
    }


    @ApiOperation("根据券方案计算订单优惠明细")
    @PostMapping("/discount")
    public CouponDiscountDTO queryDiscountDetailByOrder(
            @RequestBody OrderCouponDTO orderCouponDTO){
        //TODO 根据券方案计算订单优惠明细
//        return discountService.queryDiscountDetailByOrder(orderCouponDTO);
        return null;
    }

    @ApiOperation("核销指定优惠券")
    @PutMapping("/use")
    public void writeOffCoupon(@ApiParam("用户优惠券id集合") @RequestParam("couponIds") List<Long> userCouponIds){
//        userCouponService.writeOffCoupon(userCouponIds);
        //TODO 优惠券核销

    }

    @ApiOperation("退还指定优惠券")
    @PutMapping("/refund")
    public void refundCoupon(@ApiParam("用户优惠券id集合") @RequestParam("couponIds") List<Long> userCouponIds){
//        userCouponService.refundCoupon(userCouponIds);
        //TODO 优惠券退还

    }


    @ApiOperation("分页查询我的优惠券接口")
    @GetMapping("/rules")
    public List<String> queryDiscountRules(
            @ApiParam("用户优惠券id集合") @RequestParam("couponIds") List<Long> userCouponIds){
//        return userCouponService.queryDiscountRules(userCouponIds);
        //TODO 查询优惠券-订单使用

        return null;
    }

}
